#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"

void SLInit(SL* ps) {
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLDestroy(SL* ps) {
	if (ps->arr) {
		free(ps->arr);
	}

	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLCheckCapacity(SL* ps) {
	assert(ps);

	if (ps->capacity == ps->size) {
		int newc = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SeqListType* tmp =(SeqListType*) realloc(ps->arr,newc*sizeof(SeqListType));
		if (tmp == NULL) {
			perror("realloc");

			exit(1);
		}
		ps->capacity = newc;
		ps->arr = tmp;

	}

	
}

void SLPushBack(SL* ps,SeqListType x) {
	assert(ps);
	SLCheckCapacity(ps);

	ps->arr[ps->size++] = x;
}

void SLPushFront(SL* ps, SeqListType x) {
	assert(ps);
	SLCheckCapacity(ps);

	for (int i = ps->size; i >0; i--) {
		ps->arr[i] = ps->arr[i-1];
	}
	ps->arr[0] = x;
	++ps->size;
}

void SLShow(SL s) {
	for (int i = 0; i < s.size; i++) {
		printf("%d", s.arr[i]);
	}
	printf("\n");
}

void SLDelBack(SL* ps) {
	assert(ps);
	assert(ps->size);

	--ps->size;
}

void SLDelFront(SL* ps) {
	assert(ps);
	assert(ps->size);

	for (int i = 0; i < ps->size-1; i++) {
		ps->arr[i] = ps->arr[i+1];
	}
	--ps->size;
}

void SLInsert(SL* ps, int pos, SeqListType x) {
	SLCheckCapacity(ps);
	assert(ps);
	assert(0 <= pos && pos <= ps->size);

	for (int i = ps->size; i > pos; i--) {
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[pos] = x;
	++ps->size;
}

void SLErase(SL* ps, int pos) {
	assert(ps);
	assert(0 <= pos && pos < ps->size);

	for (int i = pos; i < ps->size-1; i++) {
		ps->arr[i] = ps->arr[i + 1];
	}
	--ps->size;
}
